<?php
/*
Plugin Name: Crossroads
Plugin URI: http://www.duanestorey.com/crossroads
Description: Integrates flickr functionality into wordpress content
Author: Duane Storey
Version: 1.1
Author URI: http://www.duanestorey.com
*/

$plugin_name = 'Crossroads';
$plugin_prefix = 'xroads_';
$the_flickr_token = get_option($plugin_prefix . 'flickr_token');

include 'flickr.php';

add_filter('the_content', 'content_filter');
add_filter('the_posts', 'posts_filter');
add_action('wp_head', 'head_hook');

function posts_filter($content)
{
	return $content;
}

function head_hook($p)
{
	// need to make it have a slash on the end
	$plugin_dir = dirname($_SERVER['PHP_SELF']);
	rtrim($plugin_dir, ' /');
	if (strlen($plugin_dir) == 1) {
		$plugin_dir = '/wp-content/plugins/crossroads';
	} else {
		$plugin_dir = $plugin_dir . '/wp-content/plugins/crossroads';
	} ?>
<script type="text/javascript">
    var GB_ROOT_DIR = "/wp-content/plugins/crossroads/js/";
</script>
	<link rel="stylesheet" href="<?php echo($plugin_dir); ?>/css/crossroads.css" type="text/css" media="screen" />
	<script type="text/javascript" src="<?php echo($plugin_dir); ?>/js/AJS.js"></script>
	<script type="text/javascript" src="<?php echo($plugin_dir); ?>/js/AJS_fx.js"></script>
	<script type="text/javascript" src="<?php echo($plugin_dir); ?>/js/gb_scripts.js"></script>
	<link rel="stylesheet" href="<?php echo($plugin_dir); ?>/js/gb_styles.css" type="text/css" media="screen" />
    <?php		
		echo('
<script type="text/javascript">
			var commentstate = 1;
			function toggleComments()
			{
				var blob = document.getElementById("photothumbdiv");
				var blob2 = document.getElementById("mainphotoarea");
				if (commentstate == 1) 
				{
					commentstate = 0;
					new Effect.BlindUp(blob2, {duration: 1});
					new Effect.BlindDown(blob, {duration: 1});
					
				} 
				else 
				{
					commentstate = 1;
					new Effect.BlindUp(blob, {duration: 1});
					new Effect.BlindDown(blob2, {duration: 1});
				}
			}
		</script>
	');
}

function showPhotosFromSet(&$some_text, $set_id, $style, $assoc_id = 0)
{
	$rsp_obj = getFlickrPhotosFromSet($set_id);
	if ($rsp_obj['stat'] == 'ok') 
	{
		showPhotos($some_text, $rsp_obj, 'photoset', $assoc_id, $style);
	}
}

function showPhoto(&$some_text, $photo_id, $style, $assoc_id = 0) 
{
	$rsp_obj = getFlickrPhotoInfo($photo_id);
	if ($rsp_obj['stat'] == 'ok') {
		$p = array();
		$p['photos'] = array();
		$p['photos']['photo'] = array();
		$p['photos']['photo'][0] = $rsp_obj['photo'];
		showPhotos($some_text, $p, 'photos', $assoc_id, $style, 1);
	}
}

function showImagesFromContent($content, &$some_text, $style = '0')
{
	$photo_id_array = array();

	$assoc_id = mt_rand();
	$done = 0;
	$temp = $content;
	$str = '<!-- flickrset: ';
	while ($done == 0) 
	{		
		$set_id = strstr($temp, $str);
		if ($set_id) 
		{
			list($i,$j,$k) = sscanf($set_id, "%s %s %s");
			if ($k) 
			{
				showPhotosFromSet($some_text, $k, $style, $assoc_id);			
			}
		} 
		else 
		{
			$done = 1;
		}
		$temp = substr($set_id, strlen($str)+1);
	}
	$done = 0;
	$temp = $content;
	$str = '<!-- flickrimage: ';
	while ($done == 0) 
	{		
		$set_id = strstr($temp, $str);
		if ($set_id) 
		{
			list($i,$j,$k) = sscanf($set_id, "%s %s %s");
			if ($k) 
			{
				showPhoto($some_text, $k, $style, $assoc_id);			
				$photo_id_array[] = $k;
			}
		} 
		else 
		{
			$done = 1;
		}

		$temp = substr($set_id, strlen($str)+1);
	}
}


function content_filter($content)
{
	$some_text = '';
	$is_single_page = 0;
	
	$div_text = '';
	if (is_single()) 
	{
		$div_text = $div_text . '<div id="photocommenttoggle"><a onClick="javascript:toggleComments()" href="#none">Toggle Photo Comments</a></div>';
		$is_single_page = 1;
	} 
	
	$photo_text = '';
	$some_text = $some_text . '<div id="mainphotoarea">';
	showImagesFromContent($content, $photo_text, 0);
	$some_text = $some_text . $photo_text . '</div>';
	
	if ($is_single_page == 1) 
	{
		$some_text = $some_text . '<div id="photothumbdiv" style="display: none;">';
		showImagesFromContent($content, $some_text, 1);
		$some_text = $some_text . '</div>';
	}

	if (strlen($photo_text) > 0) 
	{
		$content = $content . $div_text;
	}

        $some_text = $some_text . '<a href="http://static.flickr.com/119/294309231_a3d2a339b9.jpg" title="Flower" rel="gb_image[]">Show flower</a>';
	return $content . $some_text;
}


function showFlickrComments($comments, $thumbs)
{	
	$all_comments = array();
	ksort($comments);
	$str = '<div id="photocomments"><table width="80%" border="0">';

	foreach($comments as $k => $v) 
	{
		$str = $str . '<tr>';
		$str = $str . '<td width="150" valign="top" align="right">' . date('M j, Y, g:i a', $k) . '</td>';
		$str = $str . '<td width="100" valign="top" align="center"><imgsrc=' . $thumbs[$v['author']]  . ' id="crossroadsimg"  /><br />' . $v['authorname']  . '</td>';
		$str = $str . '<td>' . $v['_content'] . '</td>';
		$str = $str . '</tr>';
	}
	$str = $str . '</table></div>';
	return $str;	
}

function showPhotos(&$some_text, $rsp_obj, $prefix, $val, $style, $which=0)
{
	if ($style == 1) 
	{
		$some_text = $some_text .'<div id="photocomments"><table border="0">';
	}		

	foreach ($rsp_obj[$prefix]['photo'] as $k => $v) 
	{	
		if ($which == 0) 
		{
			$title = $v['title'];
		} 
		else 
		{	
			$title = $v['title']['_content'];
		}

		$show_photo = 1;
		if ($style == 1) 
		{
			$some_text = $some_text . '<tr><td width="100" valign="top">';
		}

		$x = 'http://farm' . $v['farm'] . '.static.flickr.com/' . $v['server'] . '/'. $v['id'] . '_' . $v['secret'] . '.jpg';
		if ($prefix == 'photoset') 
		{
			$owner = $rsp_obj[$prefix]['owner'];
		} 
		else 
		{
			$owner = $v['owner']['nsid'];
		}
		$loc = 'http://www.flickr.com/photos/' .$owner . '/' .$v['id'];

		$thumbs = array();
		if ($style == 1) 
		{
			$rsp_obj = getFlickrComments($v['id']);
			$some_text = $some_text . '<span class="onephoto">';
			$some_text = $some_text . '<a href=' . $x . ' title="&lt;a href=' .$loc . '&gt;' .$title . '&lt;/a&gt;" rel="gb_imageset[' .$val .']"><img id="crossroadsimg" width="60" title="' . $title . '" style="-moz-opacity:0.85" onMouseover="this.style.MozOpacity=1" onMouseout="this.style.MozOpacity=0.85" src=http://farm' . $v['farm'] . '.static.flickr.com/' . $v['server'] . '/'. $v['id'] . '_' . $v['secret'] . '_s.jpg></a></span>';
			$some_text = $some_text . '<td><table border="0">';
			if (isset($rsp_obj['comments']['comment'])) 
			{
				foreach ($rsp_obj['comments']['comment'] as $x => $y) 
				{
					$some_text = $some_text . '<tr>';
					if (!isset($thumbs[$y['author']])) 
					{
						$thumbs[$y['author']] = getFlickrThumbnailForContact($y['author']);
					}
					$some_text = $some_text . '<td width="120" align="center"><div><a href="http://www.flickr.com/photos/' .$y['author']. '"><img id="crossroadsimg" src="' .$thumbs[$y['author']]. '" width="40" /></a><br>' . $y['authorname'] . '</div>';
					$some_text = $some_text . '<td><div id="photocommentdate">' . date('M j Y, h:i a', $y['datecreate']) . '</div><div>' . $y['_content'] . '</div></tr>';
				}
			} 
			else 
			{
				$some_text = $some_text . '<tr><td width="120" align="center"><td valign="top">No comments</tr>';
			}
			$some_text = $some_text . '</table></td>';
		} 
		else 
		{
			$some_text = $some_text . '<span class="onephoto">';
			$some_text = $some_text . '<a href="' . $x . '" title="&lt;a href=' .$loc . '&gt;' .$title . '&lt;/a&gt;" rel="gb_imageset[' .$val .']"><img id="crossroadsimg" width="60" title="' . $title . '" style="-moz-opacity:0.85" onMouseover="this.style.MozOpacity=1" onMouseout="this.style.MozOpacity=0.85" src="http://farm' . $v['farm'] . '.static.flickr.com/' . $v['server'] . '/'. $v['id'] . '_' . $v['secret'] . '_s.jpg" /></a></span>';
		}	

		if ($style == 1) 
		{
			$some_text = $some_text . '</tr>';
		}

	}
	
	if ($style == 1) 
	{
		$some_text = $some_text . '</table></div>';
	}		
}

function showMyRecentPhotos($how_many)
{
	$some_text = '';
	$rsp_obj = getFlickrRecentPhotos($how_many);
	if ($rsp_obj['stat'] == 'ok'){
		showPhotos($some_text, $rsp_obj, 'photos', mt_rand(), 0);
	}	
	echo($some_text);
}

function showMyContacts()
{
	echo('<table><tr><td>');
	echo('<div id="contactlist">');
	$rsp_obj = getFlickrContactList();
	foreach ($rsp_obj['contacts']['contact'] as $k => $v) {
		$id = $v['iconserver'];
		if ($id == 0) {
			$image_url = 'http://www.flickr.com/images/buddyicon.jpg';
		} else {
			$image_url = 'http://static.flickr.com/' .$id. '/buddyicons/' .$v['nsid'] . '.jpg';
		}

		if ($id != 0) 
		{
			$name = $v['realname'];
			if ($name == '') 
			{
				$name = $v['username'];
			}
			echo('<div id="contact">');
			echo('<div><a href="http://www.flickr.com/photos/' .$v['nsid']. '"><img id="crossroadsimg" src="' .$image_url. '" /></a></div>');
			echo('<div>' .$name. '</div>');
			echo('</div>');
		}
	}
	echo("</div>");
	echo('</table>');
}

function my_options_subpanel()
{
	global $plugin_name;
	global $plugin_prefix;

  	if (isset($_POST['info_update'])) 
	{
		update_option($plugin_prefix . 'flickr_username', $_POST['username']);	
		if (isset($_POST['reset'])) 
		{
			update_option($plugin_prefix . 'flickr_token', '');
			update_option($plugin_prefix . 'flickr_frob', '');
		}	
	}
	else if (isset($_POST['info_flickr']))
	{
		$frob = get_option($plugin_prefix . 'flickr_frob');
		$token = getFlickrToken($frob);			
		update_option($plugin_prefix . 'flickr_token', $token);
	}

	$flickr_token = get_option($plugin_prefix . 'flickr_token');
	$flickr_username = get_option($plugin_prefix . 'flickr_username');

	echo('<div class=wrap><form method="post">');
	echo('<h2>' . $plugin_name . ' Options</h2>');

	if ($flickr_token == '')
	{
		echo('<div style="margin: 10px">');
		$frob = getFrob();
		update_option($plugin_prefix . 'flickr_frob', $frob);

		$link = getFlickrAuthURL($frob);
		echo('<a href="' . $link .'" target="_new">Authorize Flickr Access</a>');
		echo('<p>Click the above link to go to your flickr account and authorize Crossroads to access photos.  Once you have completed the process on flickr.com, please click the button below to finalize the process');
		echo('</div>');
		echo('<div class="submit"><input type="submit" name="info_flickr" value="Complete Flickr Process" /></div>');
	}
	else
	{
		echo('Flickr plugin configured successfully. If you wish to reset the flickr api information, click the box below and then Update Options.');
		echo('<div style="margin: 10px"><input type="checkbox" name="reset" value="Reset Plugin" /> Reset Flickr Data</div>');
		echo('<div class="submit"><input type="submit" name="info_update" value="Update Options" /></div>');
	}

	echo('</form></div>');
}

function add_plugin_option()
{
	global $plugin_name;
	if (function_exists('add_options_page')) 
	{
		add_options_page($plugin_name, $plugin_name, 0, basename(__FILE__), 'my_options_subpanel');
    	}	
}

add_action('admin_menu', 'add_plugin_option');

?>
